home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / nrlogind.zip / RLOGIN.C < prev    next >
C/C++ Source or Header  |  1994-02-18  |  9KB  |  425 lines

  1. /*
  2.  * Copyright (C) 1994 Nathaniel W. Mishkin
  3.  * All rights reserved.
  4.  */
  5.  
  6. /*
  7.  * Copyright (C) 1983, 1990 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39.  
  40. /*
  41.  * rlogin - remote login
  42.  */
  43.  
  44. #include <windows.h>
  45. #include <winsock.h>
  46. #include <process.h>
  47. #include <io.h>
  48. #include <fcntl.h>
  49.  
  50. #include <stdlib.h>
  51. #include <setjmp.h>
  52. #include <errno.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55.  
  56. int eight, litout, rem;
  57.  
  58. int noescape;
  59. u_char escapechar = '~';
  60.  
  61. rcmd(char **ahost, 
  62.      u_short rport, 
  63.      const char *locuser, 
  64.      const char *remuser, 
  65.      const char *cmd,
  66.      int *fd2p);
  67.  
  68. void usage(void);
  69. void doit(void);
  70. void msg(char *);
  71. void writer(void);
  72. void done(int);
  73. void echo(register char);
  74.  
  75. #define STDIN_FILENO 0
  76. #define STDOUT_FILENO 1
  77.  
  78. main(argc, argv)
  79.     int argc;
  80.     char **argv;
  81. {
  82.     struct servent *sp;
  83.     char *host, *user;
  84.     u_char getescape();
  85.     char *getenv();
  86.         WSADATA wsa_data;
  87.         DWORD err;
  88.  
  89.         user = getenv("USERNAME");
  90.     host = argv[1];
  91.  
  92.         if ((err = WSAStartup(MAKEWORD(1, 1), &wsa_data)) != 0) {
  93.             fprintf(stderr, "WSAStartup\n");
  94.             exit(1);            
  95.         }
  96.  
  97.     sp = NULL;
  98.     if (sp == NULL)
  99.         sp = getservbyname("login", "tcp");
  100.     if (sp == NULL) {
  101.         (void)fprintf(stderr, "rlogin: login/tcp: unknown service.\n");
  102.         exit(1);
  103.     }
  104.  
  105.     rem = rcmd(&host, sp->s_port, user, user, "dumb", 0);
  106.  
  107.     if (rem < 0)
  108.         exit(1);
  109.  
  110.     doit();
  111.         return 0;
  112. }
  113.  
  114. void 
  115. doit(void)
  116. {
  117.         int child;
  118.         HANDLE coutput, cinput;
  119.         void reader(void *);
  120.  
  121.         coutput = GetStdHandle(STD_OUTPUT_HANDLE);
  122.         cinput  = GetStdHandle(STD_INPUT_HANDLE);
  123.  
  124.         SetConsoleMode(cinput, 0);
  125.         SetConsoleMode(coutput, ENABLE_PROCESSED_OUTPUT);
  126.         _setmode(0, _O_BINARY);
  127.  
  128.         if ((child = _beginthread(reader, 0, NULL)) == -1) {
  129.         (void)fprintf(stderr, "rlogin: _beginthread: %s.\n", strerror(errno));
  130.         done(1);
  131.     }
  132.     writer();
  133.     msg("closed connection.");
  134.     done(0);
  135. }
  136.  
  137. void
  138. done(status)
  139.     int status;
  140. {
  141.     exit(status);
  142. }
  143.  
  144.  
  145. /*
  146.  * writer: write to remote: 0 -> line.
  147.  * ~.                terminate
  148.  * ~^Z                suspend rlogin process.
  149.  * ~<delayed-suspend char>    suspend rlogin process, but leave reader alone.
  150.  */
  151. void 
  152. writer()
  153. {
  154.     register int bol, local, n;
  155.     char c;
  156.  
  157.     bol = 1;            /* beginning of line */
  158.     local = 0;
  159.     for (;;) {
  160.         n = read(STDIN_FILENO, &c, 1);
  161.         if (n <= 0) {
  162.             if (n < 0 && errno == EINTR)
  163.                 continue;
  164.             break;
  165.         }
  166.         /*
  167.          * If we're at the beginning of the line and recognize a
  168.          * command character, then we echo locally.  Otherwise,
  169.          * characters are echo'd remotely.  If the command character
  170.          * is doubled, this acts as a force and local echo is
  171.          * suppressed.
  172.          */
  173.         if (bol) {
  174.             bol = 0;
  175.             if (!noescape && c == escapechar) {
  176.                 local = 1;
  177.                 continue;
  178.             }
  179.         } else if (local) {
  180.             local = 0;
  181.             if (c == '.') {
  182.                 echo(c);
  183.                 break;
  184.             }
  185.             if (c != escapechar)
  186.                     (void)send(rem, &escapechar, 1, 0);
  187.         }
  188.  
  189.         if (send(rem, &c, 1, 0) == 0) {
  190.             msg("line gone");
  191.             break;
  192.         }
  193.         bol = c == '\r' || c == '\n';
  194.     }
  195. }
  196.  
  197. void 
  198. echo(c)
  199. register char c;
  200. {
  201.     register char *p;
  202.     char buf[8];
  203.  
  204.     p = buf;
  205.     c &= 0177;
  206.     *p++ = escapechar;
  207.     if (c < ' ') {
  208.         *p++ = '^';
  209.         *p++ = c + '@';
  210.     } else if (c == 0177) {
  211.         *p++ = '^';
  212.         *p++ = '?';
  213.     } else
  214.         *p++ = c;
  215.     *p++ = '\r';
  216.     *p++ = '\n';
  217.     (void)write(STDOUT_FILENO, buf, p - buf);
  218. }
  219.  
  220. #if 0
  221. /*
  222.  * Send the window size to the server via the magic escape
  223.  */
  224. sendwindow()
  225. {
  226.     struct winsize *wp;
  227.     char obuf[4 + sizeof (struct winsize)];
  228.  
  229.     wp = (struct winsize *)(obuf+4);
  230.     obuf[0] = 0377;
  231.     obuf[1] = 0377;
  232.     obuf[2] = 's';
  233.     obuf[3] = 's';
  234.     wp->ws_row = htons(winsize.ws_row);
  235.     wp->ws_col = htons(winsize.ws_col);
  236.     wp->ws_xpixel = htons(winsize.ws_xpixel);
  237.     wp->ws_ypixel = htons(winsize.ws_ypixel);
  238.  
  239.     (void)send(rem, obuf, sizeof(obuf), 0);
  240. }
  241.  
  242. #endif
  243.  
  244. /*
  245.  * reader: read from remote: line -> 1
  246.  */
  247. #define    READING    1
  248. #define    WRITING    2
  249.  
  250. jmp_buf rcvtop;
  251. int ppid, rcvcnt, rcvstate;
  252. char rcvbuf[8 * 1024];
  253.  
  254. #if 0
  255.  
  256. void
  257. oob()
  258. {
  259.     struct sgttyb sb;
  260.     int atmark, n, out, rcvd;
  261.     char waste[BUFSIZ], mark;
  262.  
  263.     out = O_RDWR;
  264.     rcvd = 0;
  265.     while (recv(rem, &mark, 1, MSG_OOB) < 0)
  266.         switch (WSAGetLastError()) {
  267.         case WSAEWOULDBLOCK:
  268.             /*
  269.              * Urgent data not here yet.  It may not be possible
  270.              * to send it yet if we are blocked for output and
  271.              * our input buffer is full.
  272.              */
  273.             if (rcvcnt < sizeof(rcvbuf)) {
  274.                 n = recv(rem, rcvbuf + rcvcnt,
  275.                     sizeof(rcvbuf) - rcvcnt, 0);
  276.                 if (n <= 0)
  277.                     return;
  278.                 rcvd += n;
  279.             } else {
  280.                 n = recv(rem, waste, sizeof(waste), 0);
  281.                 if (n <= 0)
  282.                     return;
  283.             }
  284.             continue;
  285.         default:
  286.             return;
  287.     }
  288.     if (mark & TIOCPKT_WINDOW) {
  289.         /* Let server know about window size changes */
  290.         (void)kill(ppid, SIGUSR1);
  291.     }
  292.     if (!eight && (mark & TIOCPKT_NOSTOP)) {
  293.         (void)ioctl(0, TIOCGETP, (char *)&sb);
  294.         sb.sg_flags &= ~CBREAK;
  295.         sb.sg_flags |= RAW;
  296.         (void)ioctl(0, TIOCSETN, (char *)&sb);
  297.         notc.t_stopc = -1;
  298.         notc.t_startc = -1;
  299.         (void)ioctl(0, TIOCSETC, (char *)¬c);
  300.     }
  301.     if (!eight && (mark & TIOCPKT_DOSTOP)) {
  302.         (void)ioctl(0, TIOCGETP, (char *)&sb);
  303.         sb.sg_flags &= ~RAW;
  304.         sb.sg_flags |= CBREAK;
  305.         (void)ioctl(0, TIOCSETN, (char *)&sb);
  306.         notc.t_stopc = deftc.t_stopc;
  307.         notc.t_startc = deftc.t_startc;
  308.         (void)ioctl(0, TIOCSETC, (char *)¬c);
  309.     }
  310.     if (mark & TIOCPKT_FLUSHWRITE) {
  311.         (void)ioctl(1, TIOCFLUSH, (char *)&out);
  312.         for (;;) {
  313.             if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
  314.                 (void)fprintf(stderr, "rlogin: ioctl: %s.\n",
  315.                     strerror(errno));
  316.                 break;
  317.             }
  318.             if (atmark)
  319.                 break;
  320.             n = recv(rem, waste, sizeof (waste), 0);
  321.             if (n <= 0)
  322.                 break;
  323.         }
  324.         /*
  325.          * Don't want any pending data to be output, so clear the recv
  326.          * buffer.  If we were hanging on a write when interrupted,
  327.          * don't want it to restart.  If we were reading, restart
  328.          * anyway.
  329.          */
  330.         rcvcnt = 0;
  331.         longjmp(rcvtop, 1);
  332.     }
  333.  
  334.     /* oob does not do FLUSHREAD (alas!) */
  335.  
  336.     /*
  337.      * If we filled the receive buffer while a read was pending, longjmp
  338.      * to the top to restart appropriately.  Don't abort a pending write,
  339.      * however, or we won't know how much was written.
  340.      */
  341.     if (rcvd && rcvstate == READING)
  342.         longjmp(rcvtop, 1);
  343. }
  344.  
  345. #endif
  346.  
  347. /* reader: read from remote: line -> 1 */
  348. void
  349. reader(void *p)
  350. {
  351.     void oob();
  352.  
  353.     int pid = getpid();
  354.     int n, remaining;
  355.     char *bufp = rcvbuf;
  356.  
  357.     (void)setjmp(rcvtop);
  358.     for (;;) {
  359.         while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
  360.             rcvstate = WRITING;
  361.             n = write(STDOUT_FILENO, bufp, remaining);
  362.             if (n < 0) {
  363.                 if (errno != EINTR)
  364.                     return;
  365.                 continue;
  366.             }
  367.             bufp += n;
  368.         }
  369.         bufp = rcvbuf;
  370.         rcvcnt = 0;
  371.         rcvstate = READING;
  372.  
  373.         rcvcnt = recv(rem, rcvbuf, sizeof (rcvbuf), 0);
  374.         if (rcvcnt == 0)
  375.             return;
  376.         if (rcvcnt < 0) {
  377.             (void)fprintf(stderr, "rlogin: read: %d.\n",
  378.                           WSAGetLastError());
  379.             return;
  380.         }
  381.     }
  382. }
  383.  
  384. void
  385. msg(str)
  386.     char *str;
  387. {
  388.     (void)fprintf(stderr, "rlogin: %s\r\n", str);
  389. }
  390.  
  391.  
  392. void
  393. usage()
  394. {
  395.     (void)fprintf(stderr,
  396.         "usage: rlogin [ -%s]%s[-e char] [ -l username ] host\n",
  397.         "8EL", " ");
  398.     exit(1);
  399. }
  400.  
  401.  
  402. u_char
  403. getescape(p)
  404.     register char *p;
  405. {
  406.     long val;
  407.     int len;
  408.  
  409.     if ((len = strlen(p)) == 1)    /* use any single char, including '\' */
  410.         return((u_char)*p);
  411.                     /* otherwise, \nnn */
  412.     if (*p == '\\' && len >= 2 && len <= 4) {
  413.         val = strtol(++p, (char **)NULL, 8);
  414.         for (;;) {
  415.             if (!*++p)
  416.                 return((u_char)val);
  417.             if (*p < '0' || *p > '8')
  418.                 break;
  419.         }
  420.     }
  421.     msg("illegal option value -- e");
  422.     usage();
  423.     /* NOTREACHED */
  424. }
  425.